<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2017/12/23
 * Time: 下午4:58
 */

namespace Biz\module\Classes\adm;

use Biz\model\trade\Order as mOrder;
use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\module\Classes\core\SearchListAbstract;
use Medoo\Medoo;


final class OrderRightsSearch extends SearchListAbstract
{
    private $join = [];

    private $order = 'addtime desc';

    protected function buildSelect()
    {
        $this->select = '*';

    }

    protected function buildWhere()
    {
        //状态
        if ($this->params['type_refund'] > 0) {
            $this->where['type_refund'] = $this->params['type_refund'];
        }

        if ($this->params['rights_status'] > 0) {
            $this->where['rights_status'] = $this->params['rights_status'];
        }

        if ($this->params['refund_order_no']) {
            $this->where['refund_order_no'] = $this->params['refund_order_no'];
        }

        if ($this->params['order_no']) {
            $this->where['order_no'] = $this->params['order_no'];
        }

        if ($this->params['only_timeout'] == 'on') {
            $this->where['OR'] = [
                'AND #the 1' => [
                    'rights_status' => mOrderRefund::RIGHTS_STATUS_NORMAL,
                    'rights_into_timeout_time[<]' => date('Y-m-d H:i:s')
                ],
                'AND #the 2' => [
                    'rights_status' => mOrderRefund::RIGHTS_STATUS_PROOFING,
                    'rights_into_time[<]' => date('Y-m-d H:i:s', time() - mOrderRefund::RIGHT_JUDGE_TIME_OUT)
                ],
            ];
            $this->order = 'rights_into_timeout_time desc';
        }

        if ($this->params['start_date'] && $this->params['stop_date']) {
            $this->where['addtime[<>]'] = [$this->params['start_date'], date('Y-m-d', strtotime($this->params['stop_date']) + 86400)];
        }

        $this->where['rights_status[>]'] = 0;

    }

    public function getCount()
    {

        if ($this->join) {
            $count = mOrderRefund::count($this->join, "*", $this->where);
        } else {
            $count = mOrderRefund::count("*", $this->where);
        }


        return $count;
    }

    public function getList()
    {
        if ($this->join) {
            $list = mOrderRefund::select($this->join, $this->select, array_merge($this->where, ['ORDER' => Medoo::raw($this->order), 'LIMIT' => $this->limit]));
        } else {
            $list = mOrderRefund::select($this->select, array_merge($this->where, ['ORDER' => Medoo::raw($this->order), 'LIMIT' => $this->limit]));
        }

        if ($list) {

            $nowTime = time();

            $orderNoList = array_column($list, 'order_no');
            $orderList = mOrder::getOrderInfoList(['order_no' => $orderNoList], [], [], true);

            $orderList = array_column($orderList, null, 'order_no');



            if ($list) {
                foreach ($list as &$info) {
                    mOrderRefund::formatInfo($info,'adm');

                    $info['order_status'] = $orderList[$info['order_no']]['status'];
                    $info['order_status_desc'] = $orderList[$info['order_no']]['status_desc'];

                    $info['timeout_desc'] = '';

                    if ($info['rights_status'] == mOrderRefund::RIGHTS_STATUS_NORMAL) {
                        if (strtotime($info['rights_into_timeout_time']) < $nowTime) {
                            $info['timeout_desc'] = '超时';
                        }
                    }

                    if ($info['rights_status'] == mOrderRefund::RIGHTS_STATUS_PROOFING) {
                        if ((strtotime($info['rights_into_time']) + mOrderRefund::RIGHT_JUDGE_TIME_OUT) < $nowTime) {
                            $info['timeout_desc'] = '超时';
                        }
                    }

                    $info['order_goods'] = [
                        'sku_desc' => $info['sku_desc'],
                        'title' => $info['title'],
                        'cover_img' => $info['cover_img_thumb_url'],
                        'cover_img_big' => $info['cover_img_url']
                    ];


                }
            }

            return $list;
        }
    }

}
